package com.ruoyi.resume.utils.minIO;

import io.minio.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@Component
public class MinIOUtil {

    @Autowired
    private MinioClient minioClient;

    //上传文件[从本地路径上传]
    public void uploadFile(String bucketName, String objectName, String filePath) throws Exception {

        // 检查存储桶是否存在，不存在则创建
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }

        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)  // 对象存储路径（如 "user-resumes/123.pdf"）
                        .filename(filePath)  // 上传的文件路径
                        .build()
        );
    }

    //上传文件(流形式)
    public void uploadFileViaStream(MultipartFile file, String bucketName, String objectName) throws Exception {

        try {
            // 直接通过流上传，无需保存到本地
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
        } catch (Exception e) {
            throw new RuntimeException("文件上传失败: " + e.getMessage());
        }
    }

    //下载文件(下载到某个路径)
    public void downloadFile(String bucketName, String objectName, String outputPath) throws Exception {
        minioClient.downloadObject(
                DownloadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .filename(outputPath)
                        .build()
        );
    }

    //下载文件（生成链接）
    public String downloadFileViaLink(String bucketName, String objectName) throws Exception {
        try {
            return minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket(bucketName)
                            .object(objectName)
                            .expiry(7, TimeUnit.DAYS)
                            .build()
            );
        } catch (IOException e) {
            throw new RuntimeException("生成链接失败: " + e.getMessage());
        }
    }

    // 下载文件（返回流）
    public InputStream downloadFileAsStream(String bucketName, String objectName) throws Exception {
        try {
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build()
            );
        } catch (Exception e) {
            throw new RuntimeException("下载文件失败: " + e.getMessage());
        }
    }

    //删除文件
    public void deleteFile(String bucketName, String objectName) throws Exception {
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build()
        );
    }
}
